import {
  SongUrl,
  SongDetail,
  Songlyrics
} from '../../network/http'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    song_url: '',
    bac: [],
    action: {
      "method": "play"
    },
    lrcstr: '',
    lclist: [],
    Loop: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      id: options.id
    })
    this.getSongDetail()
    this.getSonglyrics()
    this.getSongDrtail()


  },

  async getSongDrtail() {
    const {
      data: res
    } = await SongUrl(this.data.id)
    this.setData({
      song_url: res.data[0].url
    })
    console.log(this.data.song_url);

  },
  async getSongDetail() {
    const {
      data: res
    } = await SongDetail(this.data.id)
    this.setData({
      bac: res.songs[0]
    })
    console.log(res);
  },
  // 控制暂停播放
  playpasue() {
    let date = this.data.action.method
    if (date === "play") {
      this.setData({
        action: {
          "method": "pause"
        }
      })
    } else {
      this.setData({
        action: {
          "method": "play"
        }
      })
    }
  },
  // 获取歌词
  async getSonglyrics() {
    const {
      data: res
    } = await Songlyrics(this.data.id)
    this.setData({
      lrcstr: res.lrc.lyric
    })
    this.spliteStr(this.data.lrcstr)

  },
  // 拆分
  spliteStr(str) {
    let lclist = []
    const lrclist = str.split("\n")
    // 正则表达式
    const reg = /\[\d{2}:\d{2}\.\d{2,3}\]/
    lrclist.forEach(item => {
      if (item != null) {
        let itemdate = item.match(reg)
        if (itemdate) {
          itemdate = itemdate[0]
          itemdate = itemdate.slice(1, -1)
          // 转化为有意义的秒
          const timelist = itemdate.split(":")
          const time0 = timelist[0]
          const time1 = timelist[1]
          // 最终的值
          const time = parseFloat(time0) * 60 + parseFloat(time1)
          const lrcstr = item.replace(reg, "")
          lclist.push([time, lrcstr])

        }
      }
      this.setData({
        lclist: lclist
      })

    })


  },
  //是否循环
  Cloop() {
    if (!this.data.Loop) {
      this.setData({
        Loop: true
      })
    } else {
      this.setData({
        Loop: false
      })
    }
console.log(this.data.Loop);
  }
})